﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repositories;
using GameSchoolApplication.Classes;
using GameSchoolApplication.Models.ViewModels;

namespace GameSchoolApplication.Controllers
{
    public class ExamController : Controller
    {
        // GET: /Exam/
        /// <summary>
        /// Gets the view for a particular exam and returns it to the user.
        /// Precondition: The user clicks the exam link
        /// Postcondition: The user has received the exam view
        /// Author: Hlynur
        /// Date: 7.5 15:30
        /// <param name="ID">The ID of the desired exams</param>
        /// <returns>The view  for the exams</returns>
        /// </summary>
        public ActionResult getExam(int examID)
        {
       
            ExamRepository rep = new ExamRepository();
            Exam test = rep.getExamByID(examID); 

            return View(test);

        }

        /// <summary>
        /// Returns the edit view for a particular exam
        /// Precondition: The user clicks the edit exam for a particular exam
        /// Postcondition: The user has received the edit view for that exam
        /// Author: Hlynur
        /// Date:  7.5 15>30
        /// <param name="ID">The ID of the desired assignment</param>
        /// <returns>The edit view for the assignment</returns>
        /// </summary>
        public ActionResult editExam(int? examID)
        {
            //Initialize the repository
            if (examID.HasValue)
            {
                ExamRepository rep = new ExamRepository();
                Exam exam = rep.getExamByID(examID.Value);
                return View(exam);
            }
            return View();
        }

        /// <summary>
        /// Submits edited exam information to the database.
        /// Precondition: The user has entered updated exam information to the database. 
        /// Postcondition: The exam information has been edited in the database
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        [HttpPost]
        public ActionResult editExam(Exam model)
        {
            //Initialize the repository
            ExamRepository rep = new ExamRepository();
            bool success = rep.editExam(model);
            if (success)
            {
                return View("win");
            }
            else
            {
                return View("fail"); 
            }
        }

        /// <summary>
        /// Returns the add question form for a particular exam
        /// Precondition: The user has clicked the add question button
        /// Postcondition: The question has been added to the exam
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        public ActionResult addQuestion(int examID)
        {
            ExamRepository rep = new ExamRepository();
            examQuestion question = new examQuestion();
            //finnur næsta númer fyrir spurninguna
            var questNum = (from q in rep.getExamQuestions(examID)
                             orderby q.qNumber descending
                             select q.qNumber).Count()+1;
            //búa til lógík sem finnur ef tölu vanntar í rununa...
            
            question.qNumber = questNum;
            question.examID = examID;
            return View(question);
        }

        /// <summary>
        /// Submits the new question to the database. 
        /// Precondition: The user has entered the question information to the form. 
        /// Postcondition: The question has been added to the database
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        [HttpPost]
        public ActionResult addQuestion(examQuestion question)
        {
            ExamRepository rep = new ExamRepository();
            bool success = rep.addQuestion(question);
            if (success)
            {
                return RedirectToAction("editExam", new { examID = question.examID });
            }
            else
            {
                return View("fail");
            }
        }

        /// <summary>
        /// Gets the question options for the given questionID
        /// precondition: the question must exist beforehand.
        /// Author: Bjarmi
        /// </summary>
        /// <param name="questionID">the id of the question in question.</param>
        /// <returns>Json object{ optionNo, optionValue}</returns>
        public ActionResult getOptionsForQuestion(int questionID)
        {
            ExamRepository rep = new ExamRepository();
            var retVal = (from o in rep.getOptionsForQuestion(questionID)
                          select new { optionNo = o.optionNo, optionValue = o.optionValue });

            return Json(retVal, JsonRequestBehavior.AllowGet);
        }

        //addQuestionOption
        /// <summary>
        /// Returns the add question option form to the user. 
        /// Precondition: The user clicks the add question option for a particular question
        /// Postcondition: The user has received the form. 
        /// Author: Krilli
        /// Edit: Bjarmi
        /// Date: 8.5
        /// </summary>
        /// <param name="questionID"></param>
        /// <returns></returns>
        public ActionResult addQuestionOption(int questionID, string optionValue)
        {
            ExamRepository rep = new ExamRepository();
            examQuestionOption option = new examQuestionOption();
            option.qID = questionID;
            option.optionValue = optionValue;
            option.optionNo = rep.getNextNumberForQuestionOption(questionID);
            bool x = rep.addQuestionOption(option);

            return Json(new { optionNo = option.optionNo, optionValue = option.optionValue }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// gets an exam question using its ID and returns it as a JSON string.
        /// Used on the #questionListItem.click event in the Exam editor.
        /// Author: Bjarmi
        /// </summary>
        /// <param name="questionID">really...?</param>
        /// <returns>Json object{qID, examID, qNumber, qtext,correctIndex</returns>
        public ActionResult getQuestionInfo(int questionID)
        {
            ExamRepository rep = new ExamRepository();
            examQuestion exQue = rep.getQuestionByID(questionID);
            //Veiðir viðeigandi upplýsingar ofan í qUeef
            QuestionInfo qUeef = new QuestionInfo { qID = exQue.qID, examID = exQue.examID.Value, 
                                                    qNumber = exQue.qNumber.Value, qText = exQue.qText, 
                                                    correctIndex = exQue.correctIndex.Value };
            return Json(qUeef, JsonRequestBehavior.AllowGet);
        }

        //getExamQuestionList
        /// <summary>
        /// Gets the questionList for the Exam editor.
        /// Called in the "loadQuestionList" function in editExam.cshtml
        /// </summary>
        /// <param name="examID">id of the exam being edited</param>
        /// <returns>JSON string containing a formatted questionList</returns>
        public ActionResult getExamQuestionList(int examID)
        {
            ExamRepository rep = new ExamRepository();
            var exQueList = rep.getExamQuestions(examID);
            var data = from eQ in exQueList
                       select new { qID = eQ.qID, qNumber = eQ.qNumber, qText = eQ.qText };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult getExamForTaking(int examID)
        {
            ExamRepository rep = new ExamRepository();
            ExamModel retValue = rep.getExamForTaking(examID);

            return View("takeExam",retValue);
        }

    }
}
